import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { diskStorage } from 'multer';
import { MulterModule } from '@nestjs/platform-express';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Student } from './views/student/entities/student.entity';
import { Role } from './views/role/entities/role.entity';
import { Department } from './views/department/entities/department.entity';
import { Workpost } from './views/workpost/entities/workpost.entity';
import { Log } from './views/log/entities/log.entity';
import { LoginModule } from './views/login/login.module';
import { MenusModule } from './views/menus/menus.module';
import { StudentModule } from './views/student/student.module';
import { RoleModule } from './views/role/role.module';
import { DepartmentModule } from './views/department/department.module';
import { WorkpostModule } from './views/workpost/workpost.module';
import { LogModule } from './views/log/log.module';
import { UsersModule } from './views/users/users.module';
import { User } from './views/users/entities/user.entity';

@Module({
  imports: [MulterModule.register({
    storage: diskStorage({
      destination: './uploads', // 文件上传目录
      filename: (req, file, cb) => {
        console.log(req, file, '上传的文件信息');
        const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9);
        cb(null, `${file.fieldname}-${uniqueSuffix}-${file.originalname}`); // 保持原始文件名
      },
    }),
    limits: {
      fileSize: 1024 * 1024 * 10, // 限制文件大小为10MB
    },
  }), TypeOrmModule.forRoot(
    {
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: '123456',
      database: 'nestjs-data',
      entities: [Student, Role, Department, Workpost, Log, User], // 实体文件
      synchronize: true, // synchronize字段代表是否自动将实体类同步到数据库
      retryDelay: 500, // 重试连接数据库间隔
      retryAttempts: 10, // 重试连接数据库的次数
      autoLoadEntities: true, // 如果为true,将自动加载实体 forFeature()方法注册的每个实体都将自动添加到配置对象的实体数组中
    }
  ), LoginModule, ConfigModule.forRoot(), MenusModule, StudentModule, RoleModule, DepartmentModule, WorkpostModule, LogModule, UsersModule],
  controllers: [AppController],
  providers: [AppService],
})

export class AppModule { }
